<?php
include_once '../../lib/config.php';
include_once '../../lib/db.php';
include_once '../../lib/utils/tag.php';
include_once '../../model/nilai.php';
include_once '../../model/detil_prestasipelanggaran.php';
include_once '../../model/prestasipelanggaran.php';
include_once '../../model/rewarddanpunishment.php';

$proc = $_REQUEST['proc'];

switch ($proc){
	case "nilai-table":
		$key = $_POST['key'];
		$orderby = $_POST['orderby'];
		include '../../view/admin/nilai/tableList.php';
		break;
		
	case "add-modal":
		include '../../view/admin/nilai/nilai_add.php';
		break;
	
	case "nilai-add-combo-bidang":
		$achiev_jenis = $_POST['achiev_jenis'];
		$grp = perpel_select2(false, "ACHIEV_JENIS='$achiev_jenis'", "ACHIEV_BIDANG ASC", "ACHIEV_BIDANG");
        while ($grp_r = mysql_fetch_assoc($grp)){
        	echo "<option value=\"".$grp_r['ACHIEV_BIDANG']."\">".$grp_r['ACHIEV_BIDANG']."</option>";
        }
		break;
	
	case "nilai-add-combo-tingkat":
		$achiev_bidang = $_POST['achiev_bidang'];
		$achiev_jenis = $_POST['achiev_jenis'];
		$deprepel = deprepel_select_corelate(false, "a.ACHIEV_JENIS='$achiev_jenis' AND a.ACHIEV_BIDANG='$achiev_bidang'", "DETACHIEV_TINGKAT ASC", "DETACHIEV_TINGKAT");
		while($row = mysql_fetch_assoc($deprepel)){
			echo "<option value=\"". $row['DETACHIEV_TINGKAT']."\">". $row['DETACHIEV_TINGKAT']."</option>";
		}
		break;
		
	case "nilai-add-combo-kategori":
		$achiev_bidang = $_POST['achiev_bidang'];
		$achiev_jenis = $_POST['achiev_jenis'];
		$detachiev_tingkat = $_POST['detachiev_tingkat'];
		$deprepel = deprepel_select_corelate(false, "a.ACHIEV_JENIS='$achiev_jenis' AND a.ACHIEV_BIDANG='$achiev_bidang' AND b.DETACHIEV_TINGKAT='$detachiev_tingkat'", "DETACHIEV_NAMA ASC", "DETACHIEV_NAMA");
		while($row = mysql_fetch_assoc($deprepel)){
			echo "<option value=\"". $row['DETACHIEV_ID']."\">". $row['DETACHIEV_NAMA']."</option>";
		}
		break;
		
	case "nilai-add-combo-reward":
		$rnp_jenis = $_POST['rnp_jenis'];
		$deprepel = rnp_select2(false, "REWARD_JENIS='$rnp_jenis'", "REWARD_NAMA ASC");
		while($row = mysql_fetch_assoc($deprepel)){
			echo "<option value=\"". $row['REWARD_ID']."\">". $row['REWARD_NAMA']."</option>";
		}
		break;
		
	case "nilai-add":
		$nilai_id = $_POST['nilai_id'];
		$nilai_point = $_POST['nilai_point'];
		$detachiev_id = $_POST['detachiev_id'];
		$reward_id = $_POST['reward_id'];
		$nilai_min= $_POST['nilai_min'];
		$nilai_max= $_POST['nilai_max'];
		$achiev_jenis=$_POST['achiev_jenis'];
		
		if (validate_input()){
			//jika kenisnya pelanggaran, nilai jadikan negatif
			if ($achiev_jenis == 'pelanggaran'){
				$nilai_point *= -1;
			}
			
			$ex = nilai_add($nilai_id, $detachiev_id, $reward_id, $nilai_point, $nilai_min, $nilai_max);
			echo json_encode(array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())));	
		}
		break;
		
	case "edit-modal":
		$nilai_id = $_POST['nilai_id'];
		include '../../view/admin/nilai/nilai_edit.php';
		break;
		
	case "nilai-edit":
		$nilai_id = $_POST['nilai_id'];
		$nilai_point = $_POST['nilai_point'];
		$detachiev_id = $_POST['detachiev_id'];
		$reward_id = $_POST['reward_id'];
		$nilai_min= $_POST['nilai_min'];
		$nilai_max= $_POST['nilai_max'];
		$achiev_jenis=$_POST['achiev_jenis'];
		
		if (validate_input()){
			//jika jenisnya pelanggaran, nilai jadikan negatif
			if ($achiev_jenis == 'pelanggaran'){
				$nilai_point *= -1;
			}
			$ex = nilai_update($nilai_id, $detachiev_id, $reward_id, $nilai_point, $nilai_min, $nilai_max);
			echo json_encode(array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())));	
		}
		break;
		
	case "nilai-delete":
		$nilai_id = $_POST['nilai_id'];
		$ex = nilai_delete($nilai_id);
		echo json_encode(array('error'=> !$ex, 'msg'=> mysql_innodb_error(mysql_errno())));
}

function validate_input(){
	$nilai_id = $_POST['nilai_id'];
	$nilai_point = $_POST['nilai_point'];
	$detachiev_id = $_POST['detachiev_id'];
	$reward_id = $_POST['reward_id'];
	$nilai_min= $_POST['nilai_min'];
	$nilai_max= $_POST['nilai_max'];
		
	if ($nilai_id === '' || is_null($nilai_id)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi ID Nilai")); return false;
	}
	if ($nilai_point === '' || is_null($nilai_point)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi Point")); return false;
	}
	else if (!is_numeric($nilai_point)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi Point dengan angka")); return false;
	}
	if ($detachiev_id === '' || is_null($detachiev_id)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi Detil Prestasi/Pelanggaran")); return false;
	}
	if ($reward_id === '' || is_null($reward_id)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi Reward/Punishment")); return false;
	}
	if ($nilai_min === '' || is_null($nilai_min)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi Range Minimal")); return false;
	}
	else if (!is_numeric($nilai_min)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi Range Minimal dengan angka")); return false;
	}
	if ($nilai_max === '' || is_null($nilai_max)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi Range Maksimal")); return false;
	}
	else if (!is_numeric($nilai_max)){
		echo json_encode(array('error'=> true, 'msg'=> "Mohon mengisi Range Maksimal dengan angka")); return false;
	}
	
	if ($nilai_min>$nilai_max){
		echo json_encode(array('error'=> true, 'msg'=> "Range Nilai salah, Range Maksimal harus lebih besar dari Range Minimal")); 		return false;
	}
	
	//cek nilai min dan max, apakah ada konflik dengan record yang lain
	$record = nilai_select2(false, "DETACHIEV_ID='$detachiev_id' AND 
									($nilai_min BETWEEN NILAI_MIN AND NILAI_MAX OR $nilai_max BETWEEN NILAI_MIN AND NILAI_MAX)");
	
	while ($row = mysql_fetch_assoc($record) ) {
		if ($row['NILAI_ID'] === $nilai_id) continue;
		echo json_encode(array('error'=> true, 'msg'=> "Range Nilai konflik, lihat NILAI dengan ID '". $row['NILAI_ID'] ."'")); 		
		return false;
	}
	return true;
}